<template>
    <div style="height:100%;">
        <vue-better-scroll class="wrapper"
                           ref="scroll"
                           :scrollbar="scrollbarObj"
                           :pullDownRefresh="pullDownRefreshObj"
                           :pullUpLoad="pullUpLoadObj"
                           :startY="parseInt(startY)"
                           @pullingUp="onPullingUp">
            <div class="form-section">
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>单位名称</label>
                    </div>
                    <div class="equipment-right-select"
                         @click="selectType">
                        <span>{{checkcompany.Name?checkcompany.Name:'请选择'}}</span>
                        <i class="iconfont icon-jinru rightmsg"></i>
                    </div>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>统计指标</label>
                    </div>
                    <group>
                        <popup-picker :data="accopt"
                                      placeholder='请选择'
                                      v-model="accoptVal"></popup-picker>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>开始时间</label>
                    </div>
                    <group>
                        <datetime v-model="startTime"></datetime>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>结束时间</label>
                    </div>
                    <group>
                        <datetime v-model="endTime"></datetime>
                    </group>
                </div>
                <div class="btns">
                    <!-- <button>保存</button> -->
                    <button class="green width200"
                            @click="getData">查询</button>
                </div>
            </div>
            <h3 v-if="SIndicators == '是否在场'">设备台数：{{allNumDetail.CSVCount}}台 在场设备：{{allNumDetail.ASVCount}}台，不在场设备：{{allNumDetail.BSVCount}}台</h3>
            <h3 v-else>设备台数：{{allNumDetail.CSVCount}}台 自有设备：{{allNumDetail.ASVCount}}台，非自有设备：{{allNumDetail.BSVCount}}台</h3>
            <ul class="list"
                v-for="(item,index) in lists"
                :key="index">
                <li>
                    <h4>{{item.DepartName}}</h4>
                    <p v-if="SIndicators == '是否在场'">(在场设备：{{item.ASVCount}}台，不在场：{{item.BSVCount}}台，总数：{{item.CSVCount}}台)</p>
                    <p v-else>(自有设备：{{item.ASVCount}}台，非自有设备：{{item.BSVCount}}台，总数：{{item.CSVCount}}台)</p>
                </li>
            </ul>
        </vue-better-scroll>
        <myMenu></myMenu>
        <companyProp :companyPropShow="companyPropShow"
                     :selectCompany="selectCompany"></companyProp>
    </div>
</template>
<script>
import VueBetterScroll from 'vue2-better-scroll'
import formatDate from '@/utils/formatDate'
import myMenu from '@/commonComponents/menu'
import companyProp from '@/commonComponents/companyProp'
export default {
    name: 'statisticAnalysis',
    data () {
        return {
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: false,
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            accopt: [['是否在场', '是否自有设备']], // 统计指标
            accoptVal: ['是否在场'],
            startTime: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}-01`,
            endTime: formatDate(),
            lists: [],
            SIndicators: '是否在场',
            allNumDetail: {
            },
            checkcompany: {}, //  单位名称
            pageIndex: 0, // 当前页
            listAllCount: 0, // 总条数
            companyPropShow: false
        }
    },
    components: {
        VueBetterScroll,
        myMenu,
        companyProp
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
        this.checkcompany = {
            Name: this.userInfo.deptname,
            Id: this.userInfo.deptid
        }
        this.handleSelect()
    },
    methods: {
        // 选择单位
        selectType () {
            // this.$router.push({ path: '/testing/lookcompany' })
            this.companyPropShow = true
        },
        // 查询数据
        handleSelect () {
            this.$vux.loading.show({
                text: '加载中...'
            })
            this.SIndicators = this.accoptVal[0]
            let params = {
                'allowPaging': false,
                'business': 'GetGeneralEquipmentCount',
                'data': {
                    ProjectID: this.checkcompany.Id, // 项目ID
                    StartDate: this.startTime, // 开始时间
                    EndDate: this.endTime, // 结束时间
                    SIndicators: this.SIndicators // 统计指标
                },
                'pageIndex': 1,
                'pageSize': 20,
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.listAllCount = res.data.count
                    this.lists = this.lists.concat(res.data.data)
                    if (this.lists.length < this.listAllCount) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                    this.allNumDetail = {}
                    this.lists.forEach(item => {
                        for (const key in item) {
                            if (key != 'DepartID' && key != 'DepartName') {
                                this.allNumDetail[key] = this.allNumDetail[key] ? this.allNumDetail[key] : 0
                                this.allNumDetail[key] += parseInt(item[key])
                            }
                        }
                    })
                    this.$forceUpdate()
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        onPullingUp () {
            this.pageIndex++
            this.handleSelect()
        },
        getData () {
            this.pageIndex = 0
            this.lists = []
            this.handleSelect()
        },
        selectCompany (item) {
            this.companyPropShow = false
            this.checkcompany = {
                Name: item.Name,
                Id: item.Id
            }
        }
    }
}
</script>
<style lang="less" scoped>
h3 {
    height: auto;
    line-height: 2em;
    color: #999;
    font-size: 14px;
    background: #ebeef2;
    text-align: left;
    padding: 10px 20px;
}
.list {
    padding-left: 20px;
    li {
        padding: 12px 0;
        border-bottom: 1px solid #efeff4;
    }
    h4 {
        line-height: 32px;
        font-size: 14px;
        color: #333;
    }
    p {
        line-height: 32px;
        font-size: 14px;
        color: #999;
        padding-right: 10px;
    }
}
</style>
